package com.maple.soa.task;

import javax.servlet.AsyncContext;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author lijian
 * @date 2020/6/24
 */
public class AsyncTask implements Runnable {
    private AsyncContext ctx = null;

    public AsyncTask(AsyncContext ctx) {
        this.ctx = ctx;
    }

    public void run() {
        try {
            //等待十秒钟，以模拟业务方法的执行
            Thread.sleep(5000);
            PrintWriter out = ctx.getResponse().getWriter();
            out.println("业务处理完毕的时间：" + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE) + ".");
            out.flush();
            ctx.complete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
